package edu.xidian.forum.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xidian.forum.dto.CommentDto;
import edu.xidian.forum.entity.Comment;
import edu.xidian.forum.entity.User;
import edu.xidian.forum.mapper.CommentMapper;
import edu.xidian.forum.service.CommentService;
import edu.xidian.forum.vo.CommentVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Value("${avatar.server}")
    private String avatarServer;

    @Override
    public List<CommentVO> getCommentsByTopicID(String topicId) {
        List<CommentVO> comments = baseMapper.getCommentsByTopicID(topicId);
        comments.forEach(comment -> comment.setAvatar(avatarServer + comment.getAvatar()));
        return comments;
    }

    @Override
    public Comment create(CommentDto dto, User user) {
        Comment comment = Comment.builder()
                .userId(user.getId())
                .content(dto.getContent())
                .topicId(dto.getTopic_id())
                .createTime(new Date())
                .build();
        this.baseMapper.insert(comment);
        return comment;
    }

}

